package com.aircms.controller.admin;

import com.aircms.common.ServerResponse;
import com.aircms.controller.BaseAdminController;
import com.aircms.pojo.Role;
import com.aircms.pojo.validation.BaseQueryFieldsValidation;
import com.aircms.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "auth/role")
public class RoleAdminController extends BaseAdminController {

    @Autowired
    private IRoleService iRoleService;

    /**
     * Display a listing of the resource.
     *
     * @return
     */
    @GetMapping(value = (""))
    public ServerResponse index(BaseQueryFieldsValidation baseQueryFields) throws Exception{
        try{
            return  iRoleService.index(baseQueryFields,getPageNum(),getPageSize());
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("查询失败");
        }
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return
     */
    @GetMapping(value = "create")
    public ServerResponse create(){
        return  ServerResponse.resResponse();
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return
     */
    @PostMapping(value = "")
    public ServerResponse store(Role role) throws Exception{
        try {
            return  iRoleService.store(role);
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("添加失败");
        }
    }


    /**
     * Display the specified resource.
     *
     * @param id
     * @return
     */
    @GetMapping(value = "{id}")
    public ServerResponse show(@PathVariable Integer id){

        return  ServerResponse.resResponse();
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param id
     * @return
     */
    @GetMapping(value = "{id}/edit")
    public ServerResponse edit(@PathVariable Integer id){

        return  ServerResponse.resResponse();
    }


    /**
     * Update the specified resource in storage.
     *
     * @return
     */
    @RequestMapping(value = "{id}",method = {RequestMethod.PUT,RequestMethod.PATCH})
    public ServerResponse update(@PathVariable Integer id,Role role) throws Exception{
        try{
            return  iRoleService.update(role);
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("更新失败");
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "{id}")
    public ServerResponse destroy(@PathVariable Integer id) throws Exception{
        try{
            return iRoleService.destroy(id);
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("删除失败");
        }
    }

    @GetMapping(value = "getAllUsAbleRole")
    public ServerResponse getAllUsAbleRole() throws Exception{
        try {
            return iRoleService.getAllUsAbleRole();
        }catch (Exception e){
            this.errorLog(this.getClass(),e);
            throw new Exception("查询角色信息失败");
        }
    }
}
